package com.module.app.base

import android.graphics.Paint
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.CallSuper
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.databinding.ViewDataBinding
import com.module.base.base.BaseExceptionViewModel
import com.module.base.base.title.OnTitleListener
import com.module.base.base.BaseVmDbFragment
import com.module.base.utils.*
import com.module.app.R
import com.module.app.base.loadsir.LoadSirReload
import com.module.app.utils.InitializationUtils
import com.module.app.utils.MySkinUtils
import com.module.frame.ext.observe
import com.module.skin.ext.skinFlow

abstract class BaseFragment<VM : BaseExceptionViewModel, DB : ViewDataBinding> :
    BaseVmDbFragment<VM, DB>(),
    OnTitleListener, LoadSirReload {

    @CallSuper
    override fun initView() {
        InitializationUtils.init()
        super.initView()
        initTitle()
        setBackground(MySkinUtils.isBg())
    }

    @CallSuper
    override fun initListener() {
        super.initListener()
        //皮肤更换
        observe(skinFlow) {
            setBackground(MySkinUtils.isBg())
        }
    }

    /**
     * 设置背景图
     */
    protected open fun setBackground(isBackground: Boolean) {

    }


    /******************************设置标题开始 */
    var tvTitle: TextView? = null
    var btnLeft1: ImageView? = null //返回
    var btnRight1: ImageView? = null//右边按钮
    var tvRight: TextView? = null //右边文字
    var layoutTitle: View? = null
    private var initTitle = false

    override fun initTitle() {
        if (initTitle) {
            return
        }
        initTitle = true
        tvTitle = mRootView.findViewById(R.id.tv_title)
        layoutTitle = mRootView.findViewById(R.id.layout_title)
        btnLeft1 = mRootView.findViewById(R.id.btn_left_1)
        btnRight1 = mRootView.findViewById(R.id.btn_right_1)
        tvRight = mRootView.findViewById(R.id.tv_right)
        if (layoutTitle != null) {
            StatusMyBarUtil.setPaddingSmart(context, layoutTitle)
        }
        if (btnLeft1 != null) {
            btnLeft1!!.setOnClickListener { activity?.onBackPressed() }
        }
    }

    override fun setMyTitle(title: String?) {
        if (tvTitle == null) {
            return
        }
        tvTitle?.text = title
        tvTitle?.paint?.style = Paint.Style.FILL_AND_STROKE
        tvTitle?.paint?.strokeWidth = 0.7f
    }

    override fun setMyTitle(resId: Int) {
        setMyTitle(resources.getString(resId))
    }

    override fun setCommonTitle(title: String?, isBack: Boolean) {
        if (tvTitle == null) {
            return
        }
        setMyTitle(title)
        tvTitle?.gravity = Gravity.LEFT
        setBackVisibility(if (isBack) View.VISIBLE else View.GONE)
        layoutTitle?.setBackgroundResource(R.color.transparent)
        if (isBack) {
            btnLeft1?.apply {
                setPadding(
                    DisplayUtils.dip2px(20f),
                    paddingTop,
                    paddingRight,
                    paddingBottom
                )
            }
            tvTitle?.apply {
                setPadding(
                    DisplayUtils.dip2px(43f),
                    paddingTop,
                    paddingRight,
                    paddingBottom
                )
                setOnClickListener { activity?.onBackPressed() }
            }

        } else {
            tvTitle?.apply {
                setPadding(
                    DisplayUtils.dip2px(20f),
                    paddingTop,
                    paddingRight,
                    paddingBottom
                )
            }
        }
    }

    override fun setCommonTitle(resId: Int, isBack: Boolean) {
        setCommonTitle(resources.getString(resId), isBack)
    }

    override fun setBackVisibility(visibility: Int) {
        if (btnLeft1 == null) {
            return
        }
        btnLeft1?.visibility = visibility
    }

    override fun setBackResource(resId: Int) {
        if (btnLeft1 == null) {
            return
        }
        btnLeft1?.setImageResource(resId)
    }

    override fun setBackBtn(onClickListener: View.OnClickListener?) {
        if (btnLeft1 == null) {
            return
        }
        btnLeft1?.setOnClickListener(onClickListener)
    }

    override fun setBackBtn(resId: Int, onClickListener: View.OnClickListener?) {
        setBackResource(resId)
        setBackBtn(onClickListener)
    }

    override fun setRightBtn(@DrawableRes resId: Int, onClickListener: View.OnClickListener?) {
        if (btnRight1 == null) {
            return
        }
        btnRight1?.visibility = View.VISIBLE
        btnRight1?.setImageResource(resId)
        btnRight1?.setOnClickListener(onClickListener)
    }

    override fun setRightText(text: CharSequence?, onClickListener: View.OnClickListener?) {
        if (tvRight == null) {
            return
        }
        tvRight?.visibility = View.VISIBLE
        tvRight?.text = text
        tvRight?.setOnClickListener(onClickListener)
    }

    override fun setRightText(@StringRes resId: Int, onClickListener: View.OnClickListener?) {
        setRightText(context?.resources?.getString(resId), onClickListener)
    }

    override fun setRightBtnVisibility(visibility: Int) {
        if (btnRight1 == null) {
            return
        }
        btnRight1?.visibility = visibility
    }

    override fun setRightTextVisibility(visibility: Int) {
        if (tvRight == null) {
            return
        }
        tvRight?.visibility = visibility
    }

    /******************************设置标题结束*************************************/

}